from rest_framework.generics import ListCreateAPIView

from apps.meiduo_admin.serializers.user_serializers import UserModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.user.models import User


# 查询和增加用户
class UserListView(ListCreateAPIView):
    serializer_class = UserModelSerializer
    pagination_class = PageNum

    def get_queryset(self):
        # 获取前段传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is "" or keyword is None:
            return User.objects.all()
        else:
            return User.objects.filter(username=keyword)
